package com.huluobo.lc.giftdemo1806a;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author Lc
 * @description: 发送礼物的动画
 * @date :2020/11/16 14:04
 */
public class GiftView extends RelativeLayout {
    //礼物飞进的动画
    private static TranslateAnimation giftLayoutInAnim;

    //icon缩放的动画
    private static ScaleAnimation iconScaleAnim;

    //用户名飞进的动画
//    private static TranslateAnimation userNameIn;

    //礼物名飞进的动画
//    private static TranslateAnimation giftNameIn;

    private AnimMessage animMessage;

    public GiftView(Context context, AnimMessage message) {
        super(context);
        animMessage = message;
        init();
    }

    public GiftView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GiftView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        giftLayoutInAnim = (TranslateAnimation) AnimationUtils.loadAnimation(getContext(),
                R.anim.gift_in);
        iconScaleAnim = (ScaleAnimation) AnimationUtils.loadAnimation(getContext(),
                R.anim.icon_scale);
//        userNameIn = (TranslateAnimation) AnimationUtils.loadAnimation(getContext(),
//                R.anim.gitf_username_in);
//        giftNameIn = (TranslateAnimation) AnimationUtils.loadAnimation(getContext(),
//                R.anim.gift_name_in);

        //最外层的线性布局
        LayoutInflater.from(getContext()).inflate(R.layout.item_gift_animal, this);
        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(layoutParams);

        TextView giftNumView = findViewById(R.id.giftNum);
        TextView sendUser = findViewById(R.id.send_user);
        TextView giftName = findViewById(R.id.gift_name);
        ImageView giftIcon = findViewById(R.id.ivGift);
        RelativeLayout giftTextLayout = findViewById(R.id.rlparent);
        ImageView userIcon = findViewById(R.id.user_icon);
        giftName.setText(String.format(getContext().getResources().getString(R.string.gift_tip),
                animMessage.getGiftName()));
        sendUser.setText(animMessage.getUserName());
        giftIcon.setImageResource(R.mipmap.live_red_packet);

        userIcon.startAnimation(iconScaleAnim);
        giftNumView.setTag(1);//给数量控件设置标记
        animMessage.setUpdateTime(System.currentTimeMillis());//设置时间标记
        setTag(animMessage);//设置view的标识
        iconScaleAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                sendUser.setVisibility(VISIBLE);
                giftName.setVisibility(VISIBLE);
                giftTextLayout.setVisibility(VISIBLE);
                giftTextLayout.startAnimation(giftLayoutInAnim);//开始执行显示礼物的动画
//                sendUser.startAnimation(userNameIn);
//                giftName.startAnimation(giftNameIn);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        giftLayoutInAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                giftNumView.setVisibility(View.VISIBLE);
                giftIcon.setVisibility(View.VISIBLE);
                giftNumView.setText("x" + giftNumView.getTag());
                startComboAnim(giftNumView);//设置一开始的连击事件
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 连击动画
     *
     * @param giftNumView
     */
    public void startComboAnim(View giftNumView) {
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(giftNumView, "scaleX", 1.8f, 1.0f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(giftNumView, "scaleY", 1.8f, 1.0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.setInterpolator(new OvershootInterpolator());
        animatorSet.playTogether(animator1, animator2);
        animatorSet.start();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                ((AnimMessage) getTag()).setUpdateTime(System.currentTimeMillis());//设置时间标记,
                // 为了让新添加的礼物可以正常的计时消失
                giftNumView.setTag((Integer) giftNumView.getTag() + 1);
                //如果当前显示中的礼物数量L<=AnimMessage中的数量,继续触发Combo动画
                if ((Integer) giftNumView.getTag() <= ((AnimMessage) getTag()).getGiftNum()) {
                    ((TextView) giftNumView).setText("x" + giftNumView.getTag());
                    startComboAnim(giftNumView);
                } else {
                    ((AnimMessage) getTag()).setComboAnimationOver(true);
                    return;
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }
}
